<?php

class LogeoController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $pintar = new Libreria_Pintar();
        
        $evt[] = array('#btningresar', 'click', 'logearme();');
        $evt[] = array('#txtusuario', 'keypress', 'return validarlogeotxt(event);');
		$evt[] = array('#txtclave', 'keypress', 'return validarlogeopass(event);');
	
		$func[] = array('$("#txtusuario").focus();');
        $pintar->IniciaScript();
        $pintar->EjecutarFuncion($func);
        $pintar->PintarEvento($evt);
        $pintar->FinScript();
    }

	public function validaringresoAction()
	{
		$path = new Zend_Session_Namespace('path');
		$url = $path->data;
		
		$username = $this->_request->getPost('txtusuario');
    	$password = $this->_request->getPost('txtclave');

    	$adapter = new Libreria_Autenticar($username, $password);
    	
    	$auth = Zend_Auth::getInstance();    		
    	$result = $auth->authenticate($adapter);
    	
		if(!$result->isValid())
		{
    		$msj =  $result->getMessages();
    		echo $msj[0];
    		return;
    	}
    	
    	echo "<script type='text/javascript'>window.open('".$url."index.php', '_self')</script>";
	}
	
    public function codigomenuAction()
    {
    	$cod = $this->_request->getPost('cod');
    	$cod2 = $this->_request->getPost('cod2');
    	
		$dcodmenu = new Zend_Session_Namespace('codmenu');
		$dcodmenu->data = $cod;
		$dcodmenu->current = $cod2;
    }
	
 	public function cambiarclaveAction()
 	{
 		$path = new Zend_Session_Namespace('path');
 		$url = $path->data;
 		
    	$pintar =  new Libreria_Pintar();
    	
    	$evt[] = array('#btncancelar', 'click', 'location.href="'.$url.'index.php";');
    	$evt[] = array('#btnaceptar', 'click', 'cambiarpass();');
    	
    	$pintar->IniciaScript();
    	$pintar->PintarEvento($evt);
    	$pintar->FinScript();
    }
    
	public function guardarnuevaclaveAction()
	{
    	$this->_helper->getHelper('ajaxContext')->initContext();
    	
    	if($this->getRequest()->isXmlHttpRequest()){
    		$this->_helper->viewRenderer->setNoRender();
    		$this->_helper->layout->disableLayout();

    		$path = new Zend_Session_Namespace('path');
    		$url = $path->data;
    		
    		$login = new Zend_Session_Namespace('login');
    		$ciduser = $login->iduser;
    		$clave = $login->pass;
    		
    		$aclave = $this->_request->getPost('antclave');
    		$nclave = $this->_request->getPost('nclave');
    		
    		if($aclave == $clave){
    			
    			$parametros[] = array('@ciduser', $ciduser);
    			$parametros[] = array('@nclave', $nclave);
    			
    			$cn = new Model_Database_Datos();
    			$result = $cn->ejecutasql('bdaudit.dbo.pxw_cambiarclave', $parametros, true);
    			
    			Zend_Auth::getInstance()->clearIdentity();
				Zend_Session::destroy();
				
    			$confirm = true;
    		}else{
    			$confirm = false;
    		}
    		
    		echo $confirm;
    	}
    }
    
	public function cerrarsesionAction()
	{
		$path = new Zend_Session_Namespace('path');
    	$url = $path->data;
		
		Zend_Auth::getInstance()->clearIdentity();
		Zend_Session::destroy();
		
		$this->_redirect($url);
	}
}

